package com.example.x.dnuifood_929dx.controller;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.x.dnuifood_929dx.R;
import com.example.x.dnuifood_929dx.bean.ResultBean;
import com.example.x.dnuifood_929dx.listener.onRetrofitListener;
import com.example.x.dnuifood_929dx.model.UserModel;

import java.io.FileInputStream;
import java.io.InputStream;

/**
 * Created by X on 2018/12/4.
 */

public class AlterUserInfoActivity extends BaseActivity  implements onRetrofitListener<ResultBean> {
    private ImageView alter_return,user_image;
    private EditText et_alter_username,et_alter_password,et_alter_telephone,et_alter_address;
    private Button alter_commit;
    private UserModel userModel;
    private LinearLayout ll_image;
    String srcPath="";
    SharedPreferences sp;

    private InputStream bis;

    @Override
    void initViews() {
        setLayout(R.layout.alteruserinfoactivity);
        sp=getSharedPreferences("photo",MODE_PRIVATE);
        et_alter_username = (EditText)findViewById(R.id.et_alter_username);
        et_alter_password = (EditText)findViewById(R.id.et_alter_password);
        et_alter_telephone = (EditText)findViewById(R.id.et_alter_telephone);
        et_alter_address = (EditText)findViewById(R.id.et_alter_adress);
        alter_commit = (Button)findViewById(R.id.alter_commit);
        alter_return = (ImageView)findViewById(R.id.alter_return);
        user_image = (ImageView)findViewById(R.id.iv_user_photo);
        ll_image = (LinearLayout) findViewById(R.id.II_image);

    }

    @Override
    void initEvents() {
        alter_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                commit(); }


        });
        alter_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish(); }
        });
        ll_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(AlterUserInfoActivity.this,PhotoActivity.class);
                startActivityForResult(intent,1); }
        });
    }

    @Override
    void initData() {
        Intent intent = getIntent();
        et_alter_username.setText(intent.getStringExtra("username"));
        et_alter_password.setText(getUser_password());
        et_alter_telephone.setText(intent.getStringExtra("telephone"));
        et_alter_address.setText(intent.getStringExtra("address"));
        SharedPreferences sp=getSPFile();
//        srcPath=sp.getString("srcPath","");
        String srcPath=sp.getString("srcPath","");
        if(!"".equals(srcPath))
        {
            setFileToImage(); }
    }

//  //  private SharedPreferences getSPFile() {
//        return  sp;
//    }


    private void commit() {
        //保存图片路径
        SharedPreferences  sp=getSPFile();

        SharedPreferences.Editor editor = sp.edit();
        //  获取编辑器
        editor.putString("srcPath", srcPath);
        editor.commit();//提交修改
        userModel = new UserModel();
        userModel.updateUserById(getUser_id(),
                et_alter_username.getText().toString(),
                et_alter_password.getText().toString(),
                et_alter_telephone.getText().toString(),
                et_alter_address.getText().toString(),
                this);
    }




    @Override
    public void onSuccess(ResultBean result, int flag) {

        if(result.getSuccess().equals("1")){
            showToast("修改成功"); finish();
        }else{ showToast("修改失败");
        }
    }

    @Override
    public void onFailure(String msg) {

    }




    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data); if(requestCode==1)
        {
            Bundle bundle=data.getExtras();
            srcPath=bundle.getString("srcPath");
            if(!srcPath.equals("")) {
                setFileToImage(); }
        } }


    private void setFileToImage() {
        try {
            FileInputStream f = new FileInputStream(srcPath);
            Bitmap bm = null;
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 8;//图片的长宽都是原来的 1/8 BufferedInputStream bis = new BufferedInputStream(f);

            bm = BitmapFactory.decodeStream(bis, null, options);
            user_image.setImageBitmap(bm);
        }catch (Exception e) {
            e.printStackTrace(); }
    }


}
